package com.zyt.controller;

import com.zyt.pojo.Result;
import com.zyt.utils.AliyunOSSOperator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Slf4j
@RestController//
public class UploadController {
    @Autowired
    private AliyunOSSOperator aliyunOSSOperator;
    /*
    * 方式一:
     */
//    @RequestMapping("/upload")
//    public Result uplpad(String name, Integer age, MultipartFile  file) throws Exception {
//        log.info("接受的参数:{},{},{}",name,age,file);
//        //获取原始上传的文件名
//        String originalFilename = file.getOriginalFilename();
//        //生成新的文件名
//        String extension = originalFilename.substring(originalFilename.lastIndexOf("."));//获取文件后缀
//        String newFilename = UUID.randomUUID().toString()+extension;
//        //保存文件
//        file.transferTo(new File("D:/image/"+newFilename));
//        return Result.success();
//    }
    /*
    * 方式二:
     */
    @PostMapping("/upload")
    public Result upload(MultipartFile file) throws Exception {
        log.info("上传的参数:{}",file);
        String url = aliyunOSSOperator.upload(file.getBytes(), file.getOriginalFilename());
        return Result.success(url);
    }
}
